import {Component, OnInit} from '@angular/core';
import {FormControl, FormGroup} from "@angular/forms";
import {RoleInfoService} from "../service/role-info.service";
import {BarnRole} from "../entity/barn-role";

@Component({
  selector: 'app-role-info',
  templateUrl: './role-info.component.html',
  styleUrls: ['./role-info.component.less']
})
export class RoleInfoComponent implements OnInit {

  validateForm = new FormGroup({
    roleName: new FormControl()
  })
  loading = false

  roles: BarnRole[]
  current = 1;
  pageSize = 20;
  count = 1000;
  pageSizes = [this.pageSize, 50, 100]

  constructor(private roleService: RoleInfoService) {
  }


  ngOnInit(): void {
    this.search()
  }

  search() {
    const param = this.validateForm.getRawValue();
    this.loading = true
    this.roleService.query(param, this.current, this.pageSize).subscribe(value => {
      this.roles = value.data
      this.current = value.current || 1
      this.count = value.count || 1
      this.loading = false
    }, () => this.loading = false)
  }

  delete(pid: string) {
    this.loading = true
    this.roleService.deleteById(pid).subscribe(value => {
      this.search()
    }, error => this.loading = false)
  }
}
